package fr;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.io.PrintWriter;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class Listener implements ServletContextListener  {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("app recharge contexte");
		
	}

	@Override
	public void contextInitialized(ServletContextEvent contextapplicatif) {
	 System.out.println("démarrage de l'application ");
	 
		Connection uneConnection = null;


		try {
			Class.forName("com.mysql.jdbc.Driver");
			
		} catch (ClassNotFoundException e) {
			
			e.printStackTrace();
		}

		String nomBDD = "formulaire";

		String UrlJdbc = "jdbc:mysql://localhost:3306/" +  nomBDD +  "?user=root&password=";
		System.out.println("l'url jdbc est : " + UrlJdbc);

		try {
			uneConnection = DriverManager.getConnection(UrlJdbc);
			System.out.println("La connection est ouverte ? " + !uneConnection.isClosed());
			
			contextapplicatif.getServletContext().setAttribute("connection", "true");
			
			//==Récupération de l'objet connexion
			contextapplicatif.getServletContext().setAttribute("cnx", uneConnection);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		

		
	}
	}

}
